String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
};

$(function(){
	
	$(".red_card").click(function(){
		$("#cardId").focus();
	});
	
	$(document).bind("keydown","return",function(){
		onSubmit();
	});
});

//取消
function cancle(){
	var $loginBtn	= $(".login_btn");
	var $tip		= $("#login_tip");
	$loginBtn.removeAttr("disabled");
	$tip.empty();
}

//用户登录提交
function onSubmit(){
	var username 	= $("#username");
	var password	= $("#password");
	var rand		= $("#rand").val();
	var $tip		= $("#login_tip");
	var $loginBtn	= $(".login_btn");
	
	
	if($loginBtn.attr("disabled")){
		return false;
	}
	$loginBtn.attr("disabled",true);
	$tip.empty().html(LOADING_IMG+" 等待验证......").show();
	
	if($.trim(username.val()) == ""){
		$tip.hide().html("用户名不能为空!").fadeIn("normal");
		username.focus();
		$loginBtn.removeAttr("disabled");
		return false;
	}
	
	if($.trim(password.val()) == ""){
		$tip.hide().html("密码不能为空!").fadeIn("normal");
		password.focus();
		$loginBtn.removeAttr("disabled");
		return false;
	}
	
	var param = {username:username.val(),password:password.val(),rand:rand};
	$.getJSON($("#contextpath").val() + "/security/loginTest",param,function(data){
		if(data.code == "01"){
			$tip.hide().html(data.message).fadeIn("normal");
			
			if(data.message.toString().indexOf("验证码") != -1){
				code.focus();
			}
			else if(data.message.toString().indexOf("用户名") != -1){
				username.focus();
			}
			
			$loginBtn.removeAttr("disabled");
		}
		else if(data.code == "00"){
			$tip.hide().html(LOADING_IMG+" 验证通过......").fadeIn("normal");
			document.forms[1].submit();
		}
	});
};

